/********************************************************************************
* File Name:	deletroom.cpp
* Description:	第7章实例训练
* Reference book:《Linux环境下Qt4图形界面与MySQL编程》，机械工业出版社.2012.1
* E_mail: openlinux2011@gmail.com
*
********************************************************************************/
#include "deletroom.h"
#include "ui_deletroom.h"
#include <QtSql>
#include <QSqlQuery>
#include <QMessageBox>
#include "databaseconnection.h"

DeletRoom::DeletRoom(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DeletRoom)
{
    ui->setupUi(this);
}

DeletRoom::~DeletRoom()
{
    delete ui;
}

void DeletRoom::on_btnDel_clicked()
{
    QString delRoomId = ui->leDelRoomId->text().trimmed();
    if(delRoomId=="")
    {
        QMessageBox::about(this,"Message","please input Id");
        return ;
    }

    if(!databaseconnection::Open())
    {
        QMessageBox::about(this,"Message","connect error");
        this->close();
    }
    else
    {
        QSqlQuery query;
        query.exec("select count(*) from house.roomInfo where roomId='"+delRoomId+"'");
        if(query.next())
        {
            if(query.value(0).toInt()==0)
            {
                QMessageBox::about(this,"message","No this Id!");

                return ;
            }
            else
            {
                query.exec("delete from house.roomInfo where roomId='"+delRoomId+"'");
                QMessageBox::about(this,"Message","delete success!");
                databaseconnection::Close();
                this->close();
            }
        }
    }

}

void DeletRoom::on_btnClr_clicked()
{
    this->close();
}
